package com.huowenjie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huowenjie.entity.Goods;
import com.huowenjie.entity.GoodsExample;
import com.huowenjie.mapper.GoodsMapper;
import com.huowenjie.util.FileUpDownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
@CrossOrigin
public class GoodsController {

    @Autowired
    GoodsMapper goodsMapper;

    @RequestMapping("goodsList")
    public PageInfo<Goods> goodsList(@RequestParam(defaultValue = "0")Integer pageNum,
                                 @RequestParam(defaultValue = "3")Integer pageSize,
                                  String names,String statuss){

        PageHelper.startPage(pageNum,pageSize);

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        if(names!=""&&names.length()>0){
            criteria.andNameLike("%"+names+"%");
        }

        if(statuss!=""&&statuss.length()>0){
            criteria.andStatusEqualTo(Integer.parseInt(statuss));
        }

        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);

        return goodsPageInfo;
    }




    @RequestMapping("add")
    public String add(Goods goods){
        goodsMapper.insert(goods);
        return "success";
    }

    @RequestMapping("dell")
    public String dell(Integer[] ids){
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            goodsMapper.deleteByPrimaryKey(id);
        }
        return "success";
    }

    @RequestMapping("downLoad")
    public String downLoad(HttpServletResponse response, HttpServletRequest request){
        String s = FileUpDownUtil.downLoad(response, request);
        return s;
    }

    @RequestMapping("fileUpdate")
    public String fileUpdate(@RequestParam("file") MultipartFile file) throws IOException {

        //上传默认文件大小是1M
        //可以早配置文件修改上传文件的大小
        String fileName=file.getOriginalFilename();
        System.out.println(file+"-----"+fileName);
        File file2=new File("D:/temp"+File.separator+fileName);
        file.transferTo(file2);
        return fileName;
    }

    @RequestMapping("look")
    public Goods look(Integer id){
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        return goods;
    }

    @RequestMapping("update")
    public String update(Goods goods){
        goodsMapper.updateByPrimaryKeySelective(goods);
        return "success";
    }






}
